package com.xmut.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmut.commonutils.R;
import com.xmut.eduservice.entity.EduChapter;
import com.xmut.eduservice.entity.EduCourse;
import com.xmut.eduservice.entity.EduCourseCollect;
import com.xmut.eduservice.entity.vo.CourseInfoVo;
import com.xmut.eduservice.entity.vo.CoursePublishVo;
import com.xmut.eduservice.entity.vo.CourseQuery;
import com.xmut.eduservice.service.EduChapterService;
import com.xmut.eduservice.service.EduCourseCollectService;
import com.xmut.eduservice.service.EduCourseService;
import com.xmut.eduservice.userCF.UserCF;
import com.xmut.eduservice.userCF.UserCFInputVo;
import com.xmut.eduservice.userCF.UserCFOutputRecommendedDegreeVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author huangchaoqin
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduCourseCollectService eduCourseCollectService;

    // 添加课程基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        System.out.println(id);
        return R.ok().data("courseId", id);
    }

    // 根据id查询信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    // 修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }


    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        CoursePublishVo courseInfoForm = eduCourseService.getCoursePublishVoById(id);
        return R.ok().data("item", courseInfoForm);
    }

    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    @GetMapping("getCourseBySubjectOneId/{id}")
    public R getCourseBySubjectOneId(@PathVariable String id) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("subject_parent_id", id);
        List<EduCourse> list = eduCourseService.list(wrapper);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "分页查询课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    CourseQuery courseQuery) {
        System.out.println(courseQuery.toString());
        Page<EduCourse> pageParam = new Page<>(page, limit);
        eduCourseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        System.out.println(records.toString());
        return R.ok().data("total", total).data("rows", records);
        // 测试
    }

    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        boolean b = eduCourseService.removeCourse(courseId);
        if (b) {
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }

    // 获得热门课程
    @GetMapping("getHotCourse")
    public R getHotCourse() {
        List<EduCourse> hotCourse = eduCourseService.getHotCourse();
        return R.ok().data("list", hotCourse);
    }

    // 获得推荐课程
    @GetMapping("getRecommendedCourse/{userId}")
    public R getRecommendedCourse(@PathVariable String userId) {

        List<UserCFInputVo> userCFInputVoList = new ArrayList<>();

        QueryWrapper queryWrapperForMenberId = new QueryWrapper();
        queryWrapperForMenberId.select("DISTINCT member_id");
        List<EduCourseCollect> member_list = eduCourseCollectService.list(queryWrapperForMenberId);


        for(int i = 0; i < member_list.size(); ++i) {
            UserCFInputVo userCFInputVo = new UserCFInputVo();
            userCFInputVo.setUserId(member_list.get(i).getMemberId());
            QueryWrapper queryWrapperForCourseId = new QueryWrapper();
            queryWrapperForCourseId.eq("member_id", member_list.get(i).getMemberId());
            List<EduCourseCollect> eduCourseCollectList = eduCourseCollectService.list(queryWrapperForCourseId);
            List<String> collectList = new ArrayList<>();
            for(int j = 0; j < eduCourseCollectList.size(); ++j) {
                collectList.add(eduCourseCollectList.get(j).getCourseId());
            }
            userCFInputVo.setCourseIdList(collectList);
            userCFInputVoList.add(userCFInputVo);
        }

        UserCF userCF = new UserCF(userCFInputVoList);
        List<UserCFOutputRecommendedDegreeVo> courseRecommendation = userCF.getCourseRecommendation(userId);

        Collections.sort(courseRecommendation);
        List<String> listScore = new ArrayList<>();
        List<EduCourse> listCourse = new ArrayList<>();

        for(int i = 0; i < 5 && i < courseRecommendation.size(); ++i) {
            EduCourse eduCourse = eduCourseService.getById(courseRecommendation.get(i).getCourseId());
            listCourse.add(eduCourse);
            listScore.add("" + courseRecommendation.get(i).getRecommendedDegree().toString());
        }

        if(listCourse.size() < 5) {
            List<EduCourse> hotCourses = eduCourseService.getHotCourse();
            for(int i = listCourse.size(); i < 5; ++i) {
                listCourse.add(hotCourses.get(i));
                listScore.add("0.80");
            }
        }

        return R.ok().data("list", listCourse).data("scores", listScore);
    }

    // 根据课程id查询所有章节
    @ApiOperation(value = "根据课程id查询所有章节")
    @GetMapping("getAllChapterByCourseId/{courseId}")
    public R getAllChapterByCourseId(@PathVariable String courseId) {

        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        List<EduChapter> list = eduChapterService.list(queryWrapper);

        return R.ok().data("list", list);
    }

    // 根据分类id查询该分类下的所有课程
    @ApiOperation(value = "根据2级分类id查询该分类下的所有课程")
    @GetMapping("getAllCourseBySubjectId/{subjectId}")
    public R getAllCourseBySubjectId(@PathVariable String subjectId) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("subject_id", subjectId);
        List<EduCourse> list = eduCourseService.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.print("111");
        return R.ok().data("list", list);
    }

    @GetMapping("searchCourse/{s}")
    public R searchCourse(@PathVariable String s) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.like("title", s);
        List<EduCourse> list = eduCourseService.list(wrapper);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "搜索")
    @GetMapping("{s}")
    public R pageQuery(@PathVariable String s) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.like("title", s);
        List<EduCourse> list = eduCourseService.list(wrapper);

        return R.ok().data("list", list);
        // 测试
    }
}

